package org.example.testdemo1.controller

import org.example.testdemo1.service.WeChatService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping("/api/wechat")
class WeChatController {

    @Autowired
    lateinit var weChatService: WeChatService


    @PostMapping("/login")
    fun login(@RequestBody requestBody: Map<String, String>): Map<String, Any>? {
        val jsCode = requestBody["js_code"] ?: ""
        val sessionData = weChatService.getAccessToken(jsCode)

        if (sessionData != null && sessionData.containsKey("openid")) {
            val accessToken = sessionData["access_token"] as String?
            val openId = sessionData["openid"] as String?

            // 获取用户信息（可选）
            if (accessToken != null && openId != null) {
                val userInfo = weChatService.getUserInfo(accessToken, openId)
              //  sessionData.putAll(userInfo!!)
            }
        }
        return null

    }



}